//----------------------------------
//-----Sweeeeet Banana SWEEEP-------
//-----------Made by Brian Nevec----
//----------------------------------
//---Part of the SMOD Weapons Pack--
//----------------------------------

//if on server
if SERVER then

	//add the file
	AddCSLuaFile("shared.lua");

	//set the hold type
	SWEP.HoldType			= "melee";

end

//if on client
if CLIENT then

	//set some stuff
	SWEP.PrintName			= "Sweeeeet Banana";
	SWEP.Author				= "Brian Nevec";
	SWEP.Slot				= 3;
	SWEP.SlotPos			= 3;
	SWEP.Purpose			= "Throws bananas";
	SWEP.Instructions		= "Left Click: Throw bananas, Right Click: Detonate";

	//set the category
	SWEP.Category			= "SMOD";
	
	//selection icon
	SWEP.WepSelectIcon = surface.GetTextureID("weapons/bananaicon");

	-- Kill Icon
	if(file.Exists("../materials/weapons/banana_killicon.vmt")) then

		killicon.Add("smod_banana","weapons/banana_killicon",Color(255,255,255));

	end 

end

//is it spawnable?
SWEP.Spawnable				= true;
SWEP.AdminSpawnable			= false;

//models
SWEP.ViewModel				= "models/weapons/v_banana.mdl";
SWEP.WorldModel				= "models/weapons/w_banana.mdl";

//more stuff
SWEP.Weight					= 25;
SWEP.AutoSwitchTo			= false;
SWEP.AutoSwitchFrom			= false;

//primary settings
SWEP.Primary =
{
	ClipSize				= -1,
	DefaultClip				= -1,
	Automatic				= true,
	Ammo					= "none"
}

//secondary settings
SWEP.Secondary =
{
	ClipSize				= -1,
	DefaultClip				= -1,
	Automatic				= false,
	Ammo					= "none"
}

SWEP.Sounds =
{
	Squeeze1 = Sound("weapons/bugbait/bugbait_squeeze1.wav"),
	Squeeze2 = Sound("weapons/bugbait/bugbait_squeeze2.wav"),
	Squeeze3 = Sound("weapons/bugbait/bugbait_squeeze3.wav")
}

if SERVER then

	//too many bananas can crash you
	SWEP.BananaLimit = CreateConVar("smod_bananalimit","30",FCVAR_NOTIFY);

end

//on initialize
function SWEP:Initialize()

	//if on server
	if SERVER then

		//set the hold type
		self:SetWeaponHoldType(self.HoldType);

	end

end

//when we want to attack with the primary
function SWEP:PrimaryAttack()

	if SERVER then

		self.Owner.BananaList = self.Owner.BananaList or {};
		self.Owner.SpawnedBananas = self.Owner.SpawnedBananas or 0;
		
		if self.Owner.SpawnedBananas < self.BananaLimit:GetInt() then
		
			local Banana = ents.Create("smod_banana");

			if ValidEntity(Banana) then

				Banana.BananaType = 1;
				Banana.Spawner = self.Owner;

				Banana:SetPos(self.Owner:GetShootPos() + self.Owner:GetAimVector() * 30);
				Banana:Spawn();
				Banana:Activate();

				local Physics = Banana:GetPhysicsObject();

				if ValidEntity(Physics) then

					Physics:ApplyForceCenter(self.Owner:GetAimVector() * math.Rand(500,800) * Physics:GetMass());

					self.Weapon:SendWeaponAnim(ACT_VM_THROW);

					local function C()

						if ValidEntity(self.Weapon) then

							self.Weapon:SendWeaponAnim(ACT_VM_DRAW);

						end

					end
					timer.Simple(0.1,C);
		
					table.insert(self.Owner.BananaList,Banana);

					self.Owner.SpawnedBananas = self.Owner.SpawnedBananas + 1;
					
				else

					Banana:Remove();

				end

			end
			
		else
		
			self.Weapon:EmitSound(Sound("buttons/combine_button_locked.wav"));

		end
	
	end
		
	//delay the fire
	self.Weapon:SetNextPrimaryFire(CurTime() + 0.14);
	self.Weapon:SetNextSecondaryFire(CurTime() + 1);
		
	//return false
	return false;

end

function SWEP:SecondaryAttack()

	if SERVER then

		timer.Simple(0.3,
		function()

			for I,B in pairs(self.Owner.BananaList or {}) do

				if ValidEntity(B) then

					timer.Simple(0.2 * I,B.DoBananas,B);
					
				end
				
				self.Owner.BananaList[I] = nil
				
				self.Owner.SpawnedBananas = self.Owner.SpawnedBananas - 1;

			end
			
			self.Owner:EmitSound(self.Sounds["Squeeze"..math.random(1,3)]);
			
			self.Weapon:SendWeaponAnim(ACT_VM_SECONDARYATTACK);

		end)

	end

	//delay the fire
	self.Weapon:SetNextPrimaryFire(CurTime() + 0.14);
	self.Weapon:SetNextSecondaryFire(CurTime() + 1);

	//return false
	return false;

end

function SWEP:Deploy()

	//run the deploy animation
	self.Weapon:SendWeaponAnim(ACT_VM_DRAW);

	//and return
	return true;

end
//when we holster
function SWEP:Holster()
	//and return
	return true;

end